<template>
  <PageContainer>
    <Tabs v-model="currentTab" @change="handleRefresh">
      <Header title="答疑管理">
        <CustomTopTips text="只有设为了精选的问题才会在供需详情展示" />
        <TabNav tabBarColor="#2878FF" />
        <RadioTagGroup
          v-model="receivedParams.isReply"
          v-if="currentTab === 0"
          :data="receivedTagList"
          @change="handleRefresh"
        />
        <RadioTagGroup
          v-model="sendParams.isReply"
          v-if="currentTab === 1"
          :data="sendTagList"
          @change="handleRefresh"
        />
      </Header>
      <TabsItem name="收到的问题" :unread="receivedNum" :value="0">
        <Paglist
          v-model="receivedDataList"
          :is-init-api="false"
          :parame="receivedParams"
          @register="receivedRegister"
          :is-show-loadmore="receivedDataList.length > 1"
          :scroll-top-bottom="200"
        >
          <view class="pa-16">
            <template v-for="(v, i) in receivedDataList" :key="v.id">
              <QuestionItem :item="v" type="received" />
            </template>
          </view>
        </Paglist>
      </TabsItem>
      <TabsItem name="发出的问题" :unread="0" :value="1">
        <Paglist
          v-model="sendDataList"
          :is-init-api="false"
          :parame="sendParams"
          @register="sendRegister"
          :is-show-loadmore="sendDataList.length > 1"
          :scroll-top-bottom="200"
        >
          <view class="pa-16">
            <template v-for="(v, i) in sendDataList" :key="v.id">
              <QuestionItem :item="v" type="sent" />
            </template>
          </view>
        </Paglist>
      </TabsItem>
    </Tabs>
  </PageContainer>
</template>

<script lang="ts" setup>
  /* questionManage 答疑管理 */
  import Header from '@/components/Header/index.vue';
  import PageContainer from '@/components/PageContainer/index.vue';
  import Tabs from '@/components/tabs/index.vue';
  import TabsItem from '@/components/tabs/item.vue';
  import TabNav from '@/components/tabs/nav.vue';
  import RadioTagGroup, { type TagItem } from '../components/RadioTagGroup.vue';
  import { onMounted, provide, reactive, ref } from 'vue';
  import { onLoad } from '@/router';
  import { cloneDeep } from 'lodash-es';
  import { onPageScroll, onShow } from '@dcloudio/uni-app';
  import Paglist from '@/components/Paglist/index.vue';
  import { useConList } from '@/components/Paglist/useConList';
  import QuestionItem from './components/QuestionItem.vue';
  import {
    getUserBoQuestionRceiveNum,
    questionReceivePageList,
    questionSendPageList,
  } from '@/api/businessOpportunities';
  import type {
    IQuestionPageListData,
    IQuestionPageListResult,
  } from '@/api/businessOpportunities/type';
  import { PublicBooleanEnum } from '@/enums/publicModuleEnum';
  import CustomTopTips from '@/components/CustomTopTips/index.vue';

  const currentTab = ref<number>(1);

  const statusTagList: TagItem[] = [
    {
      value: null,
      label: '全部',
    },
    {
      value: PublicBooleanEnum.NO,
      label: '待回复',
    },
    {
      value: PublicBooleanEnum.YES,
      label: '已回复',
    },
  ];

  function handleRefresh() {
    if (currentTab.value) {
      sendRefresh();
    } else {
      receivedRefresh();
    }
  }

  // 收到的  ------------------ start ----------------
  const receivedTagList = ref<TagItem[]>(cloneDeep(statusTagList));

  const receivedDataList = ref<IQuestionPageListResult[]>([]);
  const receivedParams = reactive<IQuestionPageListData>({
    isReply: receivedTagList.value[0].value,
  });
  const [receivedRegister, { refresh: receivedRefresh }] = useConList({
    api: questionReceivePageList,
  });

  // 收到的  ------------------ end ----------------

  // 发出的 ------------------ start ----------------
  const sendTagList = ref<TagItem[]>(cloneDeep(statusTagList));
  const sendDataList = ref<IQuestionPageListResult[]>([]);
  const sendParams = reactive<IQuestionPageListData>({
    isReply: sendTagList.value[0].value,
  });
  const [sendRegister, { refresh: sendRefresh }] = useConList({ api: questionSendPageList });

  // 发出的 ------------------ end ----------------

  onLoad<'questionManage'>((op) => {
    currentTab.value = op.currentTab === undefined ? 1 : op.currentTab;
    queryUserBoQuestionRceiveNum();
  });

  onShow(() => {
    handleRefresh();
    queryUserBoQuestionRceiveNum();
  });

  provide('api', void 0);

  onPageScroll(() => {});

  const receivedNum = ref(0);
  const queryUserBoQuestionRceiveNum = async () => {
    receivedNum.value = await getUserBoQuestionRceiveNum();
    receivedTagList.value[1].num = receivedNum.value;
  };
</script>

<style lang="scss" scoped></style>
